import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.MenuEvent;
import java.awt.*;
import java.awt.event.*;
import java.awt.Font.*;
import javax.swing.event.MenuListener;
import java.util.*;
import java.io.*;

public class Main2 extends JWindow implements ActionListener, MenuListener, WindowListener,
                                            WindowFocusListener,
                                            WindowStateListener {
    public String highlighterText;

    public static JFrame frame = new JFrame();

    public static JEditorPane editor = new JEditorPane();
    public JScrollPane editorScrollPane = new JScrollPane(editor,
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

    JMenuBar menuBar = new JMenuBar();

    JMenu fileMenu = new JMenu();
    JMenuItem fileNew = new JMenuItem();
    JMenuItem fileOpen = new JMenuItem();
    JMenuItem fileSave = new JMenuItem();
    JMenuItem quit = new JMenuItem();

    JMenu editMenu = new JMenu();
    //JMenuItem editUndo = new JMenuItem();
    JMenuItem editCopy = new JMenuItem();
    JMenuItem editCut = new JMenuItem();
    JMenuItem editPaste = new JMenuItem();
    JMenuItem selectAll = new JMenuItem();

    JMenuItem formatFonts = new JMenuItem();
    JMenu formatMenu = new JMenu();

    JRadioButtonMenuItem highlightJava = new JRadioButtonMenuItem("Java", true);
    JRadioButtonMenuItem highlightXHTML = new JRadioButtonMenuItem("HTML/XHTML", false);
    JRadioButtonMenuItem highlightCCPP = new JRadioButtonMenuItem("C/C++", false);
    JRadioButtonMenuItem highlightNone = new JRadioButtonMenuItem("None", false);
    JMenu highlightMenu = new JMenu();

    JMenuItem fullScreen = new JMenuItem();
    JMenu viewMenu = new JMenu();

    JPopupMenu popMenu = new JPopupMenu();
    JMenuItem popCopy = new JMenuItem();
    JMenuItem popCut = new JMenuItem();
    JMenuItem popPaste = new JMenuItem();
    JSeparator popSep = new JSeparator();
    JMenuItem popSelectAll = new JMenuItem();
    popupListener popupListener = new popupListener();

    public static String currentFile = new String();

    private GraphicsDevice graphicsDevice;
    private DisplayMode origDisplayMode;

        class HighlightDocumentNone extends DefaultStyledDocument
        {
                public HighlightDocumentNone()
                {
                }
        }
        class highlightKitNone extends StyledEditorKit {
            @Override
                public Document createDefaultDocument()
                {
                        return new HighlightDocumentNone();
                }
        }

        public Main2(String args[])
        {
            frame.setTitle("Untitled - nText 0.2.1.1 Beta");

            fileMenu.setText("File");

            fileNew.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
            fileNew.setText("New File");
            fileNew.addActionListener(this);
            fileMenu.add(fileNew);

            fileOpen.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
            fileOpen.setText("Open File");
            fileOpen.addActionListener(this);
            fileMenu.add(fileOpen);

            fileSave.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
            fileSave.setText("Save File");
            fileSave.addActionListener(this);
            fileMenu.add(fileSave);

            fileMenu.addSeparator();

            quit.setText("Quit");
            quit.addActionListener(this);
            fileMenu.add(quit);

            menuBar.add(fileMenu);

            editMenu.setText("Edit");
            editMenu.addMenuListener(this);

            /*editUndo.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Z, java.awt.event.InputEvent.CTRL_MASK));
            editUndo.setText("Undo");
            editMenu.add(editUndo);*/

            editCut.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.CTRL_MASK));
            editCut.setText("Cut");
            editCut.addActionListener(this);
            editCut.setEnabled(false);
            editMenu.add(editCut);

            editCopy.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.CTRL_MASK));
            editCopy.setText("Copy");
            editCopy.addActionListener(this);
            editMenu.add(editCopy);

            editPaste.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_V, java.awt.event.InputEvent.CTRL_MASK));
            editPaste.setText("Paste");
            editPaste.addActionListener(this);
            editMenu.add(editPaste);

            editMenu.addSeparator();

            selectAll.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.CTRL_MASK));
            selectAll.setText("Select All");
            selectAll.addActionListener(this);
            editMenu.add(selectAll);

            menuBar.add(editMenu);

            viewMenu.setText("View");

//            fullScreen.setAccelerator(keyStroke);
            fullScreen.setText("Full Screen");
            fullScreen.addActionListener(this);
            viewMenu.add(fullScreen);

            menuBar.add(viewMenu);

            highlightMenu.setText("Highlighter");
            
            highlightJava.setText("Java");
            highlightJava.addActionListener(this);
            highlightMenu.add(highlightJava);

            highlightXHTML.setText("HTML/XHTML");
            highlightXHTML.addActionListener(this);
            highlightMenu.add(highlightXHTML);

            highlightCCPP.setText("C/C++");
            highlightCCPP.addActionListener(this);
            highlightMenu.add(highlightCCPP);

            highlightNone.setText("None");
            highlightNone.addActionListener(this);
            highlightMenu.add(highlightNone);

            menuBar.add(highlightMenu);

            formatMenu.setText("Format");

            popCut.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.CTRL_MASK));
            popCut.setText("Cut");
            popCut.addActionListener(this);
            popMenu.add(popCut);

            popCopy.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.CTRL_MASK));
            popCopy.setText("Copy");
            popCopy.addActionListener(this);
            popMenu.add(popCopy);

            popPaste.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_V, java.awt.event.InputEvent.CTRL_MASK));
            popPaste.setText("Paste");
            popPaste.addActionListener(this);
            popMenu.add(popPaste);

            popMenu.addSeparator();

            popSelectAll.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.CTRL_MASK));
            popSelectAll.setText("Select All");
            popSelectAll.addActionListener(this);
            popMenu.add(popSelectAll);

            editor.addMouseListener(popupListener);

            frame.setJMenuBar(menuBar);

            frame.addWindowListener(this);

            editor.setFont(new Font("Courier New", Font.PLAIN, 12));
            editor.setEditorKit(new highlightKitJava());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(editorScrollPane);
            frame.setSize(640,480);
            frame.setVisible(true);
        }

        public class popupListener extends MouseAdapter {
        @Override
        public void mousePressed(MouseEvent evt) {
            if (evt.isPopupTrigger()) {
                popMenu.show(evt.getComponent(), evt.getX(), evt.getY());
                if (editor.getSelectedText() == null) {
                popCut.setEnabled(false);
                popCut.repaint();
                } else {
                    popCut.setEnabled(true);
                    popCut.repaint();
                }
                if (editor.getSelectedText() == null) {
                popCopy.setEnabled(false);
                popCopy.repaint();
                } else {
                    popCopy.setEnabled(true);
                    popCopy.repaint();
                }
            }
        }
        @Override
        public void mouseReleased(MouseEvent evt) {
            if (evt.isPopupTrigger()) {
                popMenu.show(evt.getComponent(), evt.getX(), evt.getY());
            if (editor.getSelectedText() == null) {
                popCut.setEnabled(false);
                popCut.repaint();
                } else {
                    popCut.setEnabled(true);
                    popCut.repaint();
                }
                if (editor.getSelectedText() == null) {
                popCopy.setEnabled(false);
                popCopy.repaint();
                } else {
                    popCopy.setEnabled(true);
                    popCopy.repaint();
                }
            }
        }
}
        public void windowClosing(WindowEvent e) {
        }
        public void windowClosed(WindowEvent e) {
        }
        public void windowOpened(WindowEvent e) {
        }
        public void windowIconified(WindowEvent e) {
        }
        public void windowDeiconified(WindowEvent e) {
        }
        public void windowActivated(WindowEvent e) {
        }
        public void windowDeactivated(WindowEvent e) {
        }
        public void windowGainedFocus(WindowEvent e) {
        }
        public void windowLostFocus(WindowEvent e) {
        }
        public void windowStateChanged(WindowEvent e) {
        }

        public void actionPerformed(ActionEvent evt)
        {
        Object source = evt.getSource();
        if (source == fileOpen) {
            //Thread oFT = new Thread(new openFileClass());
            //oFT.start();
        } else if (source == fileNew) {
            frame.setTitle("Untitled - nText 0.2.1.1 Beta");
            editorScrollPane.repaint();
            editor.setText("");
        } else if (source == fileSave) {
            //Thread sFT = new Thread(new saveFileClass());
            //sFT.start();
        } else if (source == quit) {
            System.exit(0);
        } else if (source == editCut) {
            editor.cut();
        } else if (source == editCopy) {
            editor.copy();
        } else if (source == editPaste) {
            editor.paste();
        } else if (source == selectAll) {
            editor.selectAll();
        } else if (source == fullScreen) {
            frame.setUndecorated(false);
            frame.setResizable(false);
            graphicsDevice.setFullScreenWindow(frame);
        } /*else if (source == highlightJava) {
            highlighterText = editor.getText();
            editor.setEditorKit(new highlightKitJava());
            editor.setText(highlighterText);
            highlightJava.setSelected(true);
            highlightXHTML.setSelected(false);
            highlightCCPP.setSelected(false);
            highlightNone.setSelected(false);
        } else if (source == highlightXHTML) {
            highlighterText = editor.getText();
            editor.setEditorKit(new HighlightDocumentXHTML());
            editor.setText(highlighterText);
            highlightJava.setSelected(false);
            highlightXHTML.setSelected(true);
            highlightCCPP.setSelected(false);
            highlightNone.setSelected(false);
        } else if (source == highlightCCPP) {
            highlighterText = editor.getText();
            editor.setEditorKit(new HighlightDocumentCCPP());
            editor.setText(highlighterText);
            highlightJava.setSelected(false);
            highlightXHTML.setSelected(false);
            highlightCCPP.setSelected(true);
            highlightNone.setSelected(false);
        } else if (source == highlightNone) {
            highlighterText = editor.getText();
            editorScrollPane.repaint();
            editor.setText(highlighterText);
            highlightJava.setSelected(false);
            highlightXHTML.setSelected(false);
            highlightCCPP.setSelected(false);
            highlightNone.setSelected(true);
        } */else if (source == popCopy) {
            editor.copy();
        } else if (source == popCut) {
            editor.cut();
        } else if (source == popPaste) {
            editor.paste();
        } else if (source == popSelectAll) {
            editor.selectAll();
        } else if (source == formatFonts) {
            //fontChanger fC = new fontChanger();
    }
}

        public static void main(String args[])
        {
            try {
        UIManager.setLookAndFeel(
            UIManager.getSystemLookAndFeelClassName());
        }
        catch (UnsupportedLookAndFeelException e) {
        }
        catch (ClassNotFoundException e) {
        }
        catch (InstantiationException e) {
        }
        catch (IllegalAccessException e) {
        }

                new Main(args);
        }
        @Override
    public void menuCanceled(MenuEvent mevt) {
    }
    @Override
    public void menuDeselected(MenuEvent mevt) {
    }
    @Override
    public void menuSelected(MenuEvent mevt) {
        if (mevt.getSource() == editMenu) {
            if (editor.getSelectedText() == null) {
                editCut.setEnabled(false);
                editCut.repaint();
            } else {
                editCut.setEnabled(true);
                editMenu.repaint();
            }
            if (editor.getSelectedText() == null) {
                editCopy.setEnabled(false);
                editCopy.repaint();
            } else {
                editCopy.setEnabled(true);
                editCopy.repaint();
            }
        }
    }
}

